<template>
  <!-- 技术规则 列表 -->
  <div :class="['tech-list', { mobile: $isMobile }]">
    <div
      class="top"
      v-if="$isMobile"
    >
      {{ t('header.menu5') }}
    </div>

    <Banner
      v-if="$isMobile"
      bannerModel="9"
    />

    <div class="g-con">
      <el-breadcrumb
        v-if="!$isMobile"
        separator=">"
        class="mt-2"
      >
        <el-breadcrumb-item :to="{ path: '/tech' }">{{ t('header.menu5') }}</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/tech', query: { ruleClass: route.query.ruleClass } }"> {{ getDictLabel('rule_class', ruleClass) }} </el-breadcrumb-item>
        <el-breadcrumb-item> {{ getLastBreadcrumb() }} </el-breadcrumb-item>
      </el-breadcrumb>
      <div
        class="tab mt-1"
        v-if="!$isMobile"
      >
        <el-tabs
          v-model="activeName"
          @tab-change="tabChange"
        >
          <el-tab-pane
            :label="typeName"
            :name="id"
            v-for="{ id, typeName } in tabsType"
            :key="id"
          ></el-tab-pane>
        </el-tabs>
      </div>

      <template v-if="$isMobile">
        <div class="tabm clearfix noscrollbar">
          <div
            :class="['item', 'flexcc', { active: searchData.ruleType === item.id }]"
            v-for="item in tabsType"
            :key="item.id"
            @click="tabChange(item.id)"
          >
            {{ item.typeName }}
          </div>
        </div>

        <div class="select">
          <el-select
            v-model="ruleClass"
            @change="getList()"
             filterable
          >
            <el-option
              v-for="item in dictData['rule_class']"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </div>
      </template>
    </div>

    <div class="list">
      <div class="g-con">
        <div
          class="item"
          v-for="item in listData"
          :key="item.id"
          @click="openUrl(`/techDetail`, {id: item.id})"
        >
          <img
            class="pic"
            :src="item.ruleImage"
          />

          <div>
            <div
              class="title line-2"
              :title="item.ruleTitle"
            >
              {{ item.ruleTitle }}
            </div>
            <div class="time">{{ item.pubUserName }} {{ dateFormat(item.pubTime, 3) }}</div>
            <div class="text noscrollbar line-5">{{ toText(item.ruleContent, 300) }}</div>
          </div>
        </div>
      </div>

      <div class="flexcc mt-7">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          v-model:current-page="page.pageNum"
          :page-size="page.pageSize"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script setup>
import { nextTick, onBeforeMount, ref, watch, getCurrentInstance } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { getRuleTypeListApi, getListApi } from '@/service/tech'
import { dateFormat, toText, openUrl } from '@/utils'
import useNews from '@/stores/news'
import useDict from '@/hooks/useDict'

const { dictData, getDictData, getDictLabel } = useDict()
const { $isMobile } = getCurrentInstance().appContext.config.globalProperties

const router = useRouter()
const route = useRoute()

const { t, locale } = useI18n()
const store = useNews()

watch(locale, (n) => {
  init()
})
onBeforeMount(() => {
  init()
})

const ruleClass = ref(route.query.ruleClass)
const getLastBreadcrumb = () => {
  const obj = {
    1: t('tech.t1'),
    2: t('tech.t2'),
    3: t('tech.t3')
  }
  return obj[route.query.type]
}

// tab
const activeName = ref('')
const tabsType = ref([])
const setTabData = () => {
  // 0 最新规则,1 合规技术,2 各方行动
  const obj = {
    1: 0,
    2: 1,
    3: 2
  }
  getRuleTypeListApi({
    parentType: obj[route.query.type]
  }).then(({ data }) => {
    tabsType.value = data
  })
}
// 类型
const tabChange = (name) => {
  searchData.value.ruleType = name
  getList()
}

// 搜索
const searchData = ref({})

// 列表
const listData = ref([])
const page = ref({
  pageNum: 1, // 当前页码
  pageSize: 8 // 每页条数
})
const total = ref(0) // 总数
const getList = () => {
  const obj = {
    1: 0,
    2: 1,
    3: 2
  }
  const params = { ...page.value, ...searchData.value, ruleClass: ruleClass.value,parentType: obj[route.query.type] }

  getListApi(params).then(({ rows, total: t }) => {
    listData.value = rows
    total.value = t
  })
}
// 分页
const handleCurrentChange = (val) => {
  getList()
}

// init
const init = async () => {
  getDictData('rule_class')
  setTabData()
  getList()
}
</script>

<style scoped lang="scss">
.tech-list {
  margin-bottom: -76px;
  .tab {
    border-bottom: 2px solid #eaebec;

    :deep(.el-tabs) {
      .el-tabs__header {
        margin: 0;
      }
      .el-tabs__nav-wrap::after {
        height: 0;
      }
    }
  }

  .list {
    background: #f5f5f5;
    padding-bottom: 30px;

    .g-con {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 30px;
      padding: 30px 0;
    }
    .item {
      padding: 20px;
      background: #fff;
      color: #6a6973;
      display: grid;
      grid-template-columns: 170px auto;
      gap: 24px;
      cursor: pointer;
      .pic {
        width: 100%;
        height: 170px;
        object-fit: cover;
      }
      .title {
        color: var(--titleColor);
        font-weight: bold;
        font-size: 18px;
        margin-top: 3px;
      }
      .time {
        font-size: 12px;
        margin-top: 13px;
      }
      .text {
        margin-top: 16px;
        font-size: 12px;
        line-height: 1.4;
      }
    }
  }

  &.mobile {
    margin-bottom: 0;
    .g-con {
      width: 100%;
    }

    .top {
      background: #252628;
      color: #fff;
      padding: 12px 20px;
      font-weight: bold;
    }

    .tabm {
      white-space: nowrap;
      overflow-x: scroll;
      .item {
        display: inline-block;
        padding: 10px 20px;
        border-bottom: 2px solid #eaebec;
        &.active {
          color: var(--mainColor);
          border-color: var(--mainColor);
        }
      }
    }

    .select {
      padding: 20px;
      :deep(.el-select) {
        width: 100%;
      }
    }

    .list {
      display: block;
      padding: 10px;
      background: #fff;
      .g-con {
        display: block;
        padding-top: 0;
      }
      .item {
        margin-bottom: 20px;
        padding: 0;
        grid-template-columns: 140px auto;

        img {
          height: 90px;
        }

        .title {
          -webkit-line-clamp: 1;
          margin-top: 5px;
        }
        .text {
          -webkit-line-clamp: 2;
          margin-top: 5px;
        }
      }
    }
  }
}
</style>
